﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
namespace Webcar.Controllers
{
    public partial class CarController : BaseControler
    {
        public ActionResult Move(string carno)
        {
            BS_CAR_MOVE model = new BS_CAR_MOVE();

            var car = WebcarDataProvider.DB.BS_CAR_RECORD.Find(carno);
            model.CAR_NO = carno;
            model.OLD_DEPT = car.DEPT_CODE;
            model.DEPT_CODE = car.DEPT_CODE;
            model.MOVE_DATE = DateTime.Now;
            model.OPERATOR = CurrentUser.GetLoginUserName();
            ViewBag.CarRecord = car.RECORD_NO;
            var dept = WebcarDataProvider.DB.sys_dept.Find(car.DEPT_CODE);
            ViewBag.DeptName = dept.deptname;
            return PartialView(model);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult MoveSave(BS_CAR_MOVE model)
        {
            try
            {
                CurrentUser.EnsurePermission(PermissionManager.调动车辆);
                string new_child_dept = this.Request.Form["NEW_CHILD_DEPT"];
                if (model.DEPT_CODE.IsNull())
                {
                    return Content("请选择新单位。");
                }
                if (new_child_dept.IsNull())
                {
                    return Content("请选择新下级单位。");
                }

                model.OPERATOR = CurrentUser.GetLoginUserName();
                model.OPER_DATE = DateTime.Now;
                model.MOVE_NO = "M" + WebcarDataProvider.DB.GetSearialId().ToString();

                using (var DB = new WebcarContext())
                {
                    var car = DB.BS_CAR_RECORD.Find(model.CAR_NO);
                    if (car == null)
                    {
                        return Content("错误:未找到车辆。");
                    }
                    if (car.DEPT_CODE == model.DEPT_CODE)
                    {
                        return Content("错误:调动单位不能与原单位相同。");
                    }
                    car.DEPT_CODE = model.DEPT_CODE;
                    car.CHILD_DEPT = new_child_dept;
                    DB.BS_CAR_MOVE.Add(model);
                    DB.SaveChanges();
                }
                return SuccessAlertThenReload();
            }
            catch (Exception ex)
            {
                return Content("错误:" + ex.Message);
            }
        }
        public ActionResult MoveList()
        {
            DateTime start_date = DateTime.Now.AddMonths(-1);
            DateTime end_date = DateTime.Now;
            ViewBag.StartDate = start_date;
            ViewBag.EndDate = end_date;

            if (this.Request.Form["start_date"] != null && this.Request.Form["end_date"] != null)
            {
                start_date = this.Request.Form["start_date"].ToDateTime();
                end_date = this.Request.Form["end_date"].ToDateTime();
            }

            var list = from p in WebcarDataProvider.DB.BS_CAR_MOVE
                       join p2 in WebcarDataProvider.DB.BS_CAR_RECORD
                       on p.CAR_NO equals p2.CAR_NO
                       join p3 in WebcarDataProvider.DB.sys_dept
                       on p.DEPT_CODE equals p3.deptcode
                       join p4 in WebcarDataProvider.DB.sys_dept
                       on p.OLD_DEPT equals p4.deptcode
                       where p.MOVE_DATE >= start_date && p.MOVE_DATE <= end_date
                       select new { 
                           MoveNo=p.MOVE_NO,
                           RecordNo=p2.RECORD_NO,
                           NewDept = p3.deptname,
                           OldDept=p4.deptname,
                           MoveDate = p.MOVE_DATE,
                           OperDate = p.OPER_DATE,
                           Operator = p.OPERATOR,
                           Cause = p.CAUSE
                       };
            ViewBag.StartDate = start_date;
            ViewBag.EndDate = end_date;

            return View(model:list);
        }
    }
}